﻿// ===============================================================================
// Copyright(c) 2007-2020 西安交通信息投资营运有限公司 版权所有
// 作者:马星俊
// ===============================================================================

using System.ComponentModel;
using System.Linq;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using XCI.Core;
using XCI.Helper;

namespace XCI.Windows
{
    [System.ComponentModel.DesignerCategory("Code")]
    public class DicTreeListLookUpEdit : TreeListLookUpEdit
    {
        /// <summary>
        /// 字典编码
        /// </summary>
        [Category("XCI"),Description("字典编码"),DefaultValue(null)]
        public string DicCode { get; set; }

        protected override void OnCreateControl()
        {
            base.OnCreateControl();
            if (WinFormHelper.IsDesignMode()) return;

            this.Properties.ValueMember = "Value";
            this.Properties.DisplayMember = "Name";
            this.Properties.PopupWidthMode = PopupWidthMode.UseEditorWidth;
            var treelist = this.Properties.TreeList;
            if (treelist == null) return;
            treelist.Columns.AddRange(new[] {
                new DevExpress.XtraTreeList.Columns.TreeListColumn
                {
                    Caption = "名称",
                    FieldName = "Name",
                    Name = "treeColName",
                    Visible = true,
                    VisibleIndex = 0
                }});
            treelist.KeyFieldName = "Id";
            treelist.ParentFieldName = "ParentId";
            treelist.OptionsSelection.EnableAppearanceFocusedCell = false;
            treelist.OptionsView.FocusRectStyle = DevExpress.XtraTreeList.DrawFocusRectStyle.None;
            treelist.OptionsView.ShowColumns = false;
            treelist.OptionsView.ShowHorzLines = false;
            treelist.OptionsView.ShowIndicator = false;
            treelist.OptionsView.ShowVertLines = false;
            treelist.OptionsView.ShowFilterPanelMode = DevExpress.XtraTreeList.ShowFilterPanelMode.Never;
            treelist.OptionsView.ShowAutoFilterRow = true;
            treelist.OptionsMenu.EnableNodeMenu = false;
            treelist.OptionsFilter.FilterMode = DevExpress.XtraTreeList.FilterMode.EntireBranch;
            treelist.OptionsBehavior.AutoPopulateColumns = true;
            treelist.StateImageList = GetDicImageCollection();
            treelist.GetStateImage += (s, e) =>
            {
                var index = 0;
                var node = e.Node;
                if (node.HasChildren && node.Expanded) index = 1;
                else if (node.HasChildren) index = 2;
                e.NodeImageIndex = index;
            };
            if (string.IsNullOrEmpty(DicCode)) return;
            LoadDic(DicCode);
        }

        /// <summary>
        /// 重新加载数据字典
        /// </summary>
        /// <param name="code">字典编码</param>
        public void LoadDic(string code)
        {
            if (WinFormHelper.IsDesignMode()) return;
            if (this.IsDisposing || this.IsDisposed || Properties.TreeList == null) return;
            var items = App.Handler.GetDic(code).Items.ToList();
            Properties.DataSource = items;
        }

        public static SvgImageCollection GetDicImageCollection()
        {
            var imageCollection1 = new DevExpress.Utils.SvgImageCollection();
            if (WinFormHelper.IsDesignMode()) return imageCollection1;
            imageCollection1.Insert(0, "actions_book", R.DicTreeSvgImage);
            imageCollection1.Insert(1, "folder_open", R.TreeOpenFolderSvgImage);
            imageCollection1.Insert(2, "folder_close", R.TreeCloseFolderSvgImage);
            return imageCollection1;
        }
    }
}